package com.yyaccp.appinfo.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author 天天向上 （john.yi@qq.com）
 * @date 2020/11/25.
 */
public class AuthInterceptor implements HandlerInterceptor {
    Logger logger = Logger.getLogger(getClass());
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("request:" + request.getRequestURI());
        if(request.getSession().getAttribute(Constants.LOGIN_USER) == null) {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String json = JSONObject.toJSONString(AjaxResult.error("请先登录"));
            out.print(json);
            out.flush();
            return false;
        }
        return true;
    }
}
